package org.ttitfly.ttcore.service.menu;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.ttitfly.ttcore.dao.menu.MenuDao;
import org.ttitfly.ttcore.util.dozer.DozerHelper;
import org.ttitfly.ttcore.util.log.LogMessageUtil;
import org.ttitfly.ttcore.vo.menu.MenuItemVO;

import com.best.oasis.tablemat.menu.MenuComponent;
import com.best.oasis.tablemat.menu.MenuRepository;

/**

 */
//Spring Bean的标识.
@Component
//默认将类中的所有函数纳入事务管理.
@Transactional
public class MenuManager {
	@Autowired
	private MenuDao menuDao;

	private static Logger logger = LoggerFactory.getLogger(MenuManager.class);
	
	public List<MenuItemVO> getAll() {
        try {
            List<MenuItemVO> ret = DozerHelper.convertList(menuDao.getAll(), MenuItemVO.class);
            return ret;
        } catch (Exception e) {
            logger.error(LogMessageUtil.printStack(e));
        }
        return null;
    }

	@SuppressWarnings("unchecked")
    public MenuRepository getRepository(HttpServletRequest request, List<MenuItemVO> menuItemList, String username) {
        MenuRepository repository = new MenuRepository();
        Set<String> parentSet = new HashSet<String>();
        String contextPath = request.getContextPath();

        for (int i = 0; i < menuItemList.size(); i++) {
            MenuItemVO vo = menuItemList.get(i);
            MenuComponent menu = new MenuComponent();

            vo2menu(vo, menu, contextPath);
            String parent = vo.getParentName();
            if (parent != null) {
                MenuComponent parentMenu = repository.getMenu(parent);
                if (parentMenu != null) {
                    repository.addMenu(parentMenu);
                    parentSet.add(parent);
                }
                menu.setParent(parentMenu);
            }
            repository.addMenu(menu);
        }
        List<String> topList = repository.getTopMenusNames();
        for (String node : topList) {
            if (!parentSet.contains(node)) {
                repository.removeMenu(node);
            }
        }

        for (int i = 0; i < menuItemList.size(); i++) {
            MenuItemVO vo = menuItemList.get(i);
            String parent = vo.getParentName();
            if (parent != null && !parentSet.contains(parent)) {
                repository.removeMenu(vo.getName());
            }
        }

        return repository;
    }
	public void vo2menu(MenuItemVO vo, MenuComponent menu, String contextPath) {
        menu.setName(vo.getName());
        menu.setTitle(vo.getTitle());
        menu.setDescription(vo.getDescription());
        menu.setTarget(vo.getTarget());
        menu.setOnclick(vo.getOnclick());
        menu.setOnmouseout(vo.getOnmouseout());
        menu.setOnmouseover(vo.getOnmouseover());

        String location = vo.getLocation();
        if (location == null) {
            location = "";
        }

        String webContext = null;
        if (webContext == null || webContext.equals("")) {
            webContext = contextPath;
        }

        // url
        if (location.indexOf(":") > -1) {
            webContext = null;
        } else {
            String topMenu = MenuRepository.TOP_PARAMETER + "=";
            String subMenu = MenuRepository.SUB_PARAMETER + "=" + vo.getName();
            if (vo.getParentName() == null) {
                topMenu += vo.getName();
            } else {
                topMenu += vo.getParentName();
            }
            if (location.indexOf(MenuRepository.TOP_PARAMETER) < 0 && topMenu.length() > 0) {
                location += (location.indexOf("?") > -1) ? "&" : "?";
                location += topMenu;
            }
            if (location.indexOf(MenuRepository.SUB_PARAMETER) < 0 && subMenu.length() > 0) {
                location += (location.indexOf("?") > -1) ? "&" : "?";
                location += subMenu;
            }
        }
        menu.setWebContext(webContext);
        menu.setLocation(location);
    }
	
	
	

}
